<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {
	public $uses = array(
		'Menus',
		'Category',
		'Support',
		'News',
		'LegelAdvices',
		'Faqs',
		'Configuration',
		'Advertise',
	);
	public $paginate = array(
		'limit' => 5,
		'order' => array('User.id' => 'Desc')
	);
	public $helpers = array('Js', 'Common', 'Counter');
	/**
	 * Components
	 * @var array
	 */
	public $components = array(
		'RequestHandler',
		'Common',
		'Breadcrumb',
	);

	public $company;
	public $gmail;

	public function beforeFilter(){
		parent::beforeFilter();
		$company = $this->Configuration->find('all', array(
				'fields' => '*',
				'order' => array('id DESC'),
				'limit' => 1,
		));
		$company = reset($company);
		$this->company = $company;
		$this->gmail = array(
				'host' => 'ssl://smtp.gmail.com',
				'port' => 465,
				'username' => $this->company['Configuration']['gmail_name'],
				'password' => $this->company['Configuration']['gmail_pass'],
				'transport' => 'Smtp'
		);

		$menuTop = array();
		$listMenu = $this->Menus->getListMenu();
		foreach ($listMenu as $menus) {
			$menuTop[] = $menus['Menus'];
		}

		$menuTop = CommonComponent::menuGroupByPositionAndLevel($menuTop);
		//left block
		$showLeftBlock = Configure::read('show_left_block');

		$leftBlock = array();
		foreach ($showLeftBlock as $catType => $title) {
			$categorys = $this->Category->getListCategoryType("'". $catType ."'");
			foreach ($categorys as $category) {
				$catId = $category['categories_multi']['id'];
			}

			$news = $this->News->find("all",
					array(
							'fields' => 'id, name, category_id, new_rewrite',
							'conditions' => array('category_id' => $catId, 'active' => 1, 'del_flg' => 0),
							'order' => array('last_update ASC'),
							'limit' => 15,
					)
			);

			$leftBlock[$catType] = $news;
		}

		//right block

		//tu van phap luat
		$legelAdvices = $this->LegelAdvices->find("all",
				array(
						'fields' => 'id, name, category_id, support_name, support_skype, support_yahoo, support_phone',
						'conditions' => array('active' => 1, 'del_flg' => 0),
						'order' => array('last_update DESC'),
						'limit' => 15,
				)
		);

		//tin moi cap nhat
		$sql = "SELECT News.id, News.category_id, News.name, News.new_rewrite,News.new_picture, Category.cat_name, Category.cat_parent_rewrite, Category.cat_rewrite, News.last_update
				FROM news as News, categories_multi as Category
				WHERE News.category_id = Category.id
				AND Category.cat_type = 'news'
				AND News.active = 1 AND Category.cat_active = 1
				AND News.del_flg = 0
				ORDER BY News.last_update DESC
				LIMIT 3";

		$newsUpdate = $this->News->query($sql);

		//get all support online
		$support = $this->Support->getAllSupport();
		//get all banner
		$allBanner = array();
		$banners = $this->Advertise->getAllBanner();
		foreach ($banners as $banner){
			$allBanner[] = $banner['advertise'];
		}

		$allBanner = CommonComponent::classifyArray($allBanner, 'ban_type');

		//get hoi dap
		$faqs = $this->Category->find("all",
					array(
							'fields' => 'id, cat_name, cat_rewrite, cat_parent_rewrite',
							'conditions' => array('cat_type' => 'faq', 'cat_parent_id !=' => 0, 'cat_active' => 1),
							'order' => array('last_update ASC'),
							'limit' => 15,
					)
		);
		//breadcrumb for TOP page
		$uri_base = Router::url( $this->here, true );
		BreadcrumbComponent::set('Trang chủ', '/');

		//set view
		$this->set(
			array(
				'company' => $company,
				'menuTop' => $menuTop,
				'leftBlock' => $leftBlock,
				'leftBlockTitle' => $showLeftBlock,
				'legelAdvices' => $legelAdvices,
				'newsUpdate' => $newsUpdate,
				'support' => $support,
				'faqs' => $faqs,
				'allBanner' => $allBanner,

			)
		);
	}
}
